import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86155
 * date: 2024-04-11
 * Time: 20:16
 *
 * @author
 */
class MtThread extends Thread{
    @Override
    public void run() {
        //线程代码
        for (int i = 0; i < 2; i++) {
            System.out.println("线程 " + Thread.currentThread().getId() + " 在运行，i的值为：" + i);
            try {
                Thread.sleep(1000);//睡眠1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        //创建线程对象
        MtThread thread1 = new MtThread();
        MtThread thread2 = new MtThread();
        //启动线程
        thread1.start();
        thread2.start();
    }
}
class MyRunnable implements Runnable{
    @Override
    public void run(){
        for (int i = 0; i < 2; i++) {
            System.out.println("线程 " + Thread.currentThread().getName() + " 在运行，i的值为：" + i);
            try{
                Thread.sleep(1000);//睡眠1秒
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        //创建Runnable对象
        MyRunnable runnable1 = new MyRunnable();

        //创建Thread对象，并将Runnable对象作为参数传递给Thread的构造函数
        Thread thread1 =new Thread(runnable1,"线程1");
        Thread thread2 =new Thread(runnable1,"线程2");
        //启动
        thread1.start();
        thread2.start();
    }
}
public class Test {

}

class MyCallable implements Callable<Integer> {
    private final int number;

    public MyCallable(int number) {
        this.number = number;
    }

    @Override
    public Integer call() throws Exception {
        // 这里是线程需要执行的代码
        int sum = 0;
        for (int i = 1; i <= number; i++) {
            sum += i;
            Thread.sleep(100); // 模拟耗时操作
        }
        return sum;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        // 提交Callable任务并获取Future对象
        Future<Integer> future1 = executorService.submit(new MyCallable(10));
        Future<Integer> future2 = executorService.submit(new MyCallable(20));

        // 获取Callable的返回值
        Integer result1 = future1.get(); // get()方法会阻塞，直到任务完成
        Integer result2 = future2.get();

        System.out.println("线程1的计算结果：" + result1);
        System.out.println("线程2的计算结果：" + result2);

        // 关闭ExecutorService
        executorService.shutdown();
    }
}
class OddEvenPrinter {
    private int number = 1;
    private final Object lock = new Object();
    private boolean isOddTurn = true; // 标记是否是奇数的回合

    public void printOdd() {
        while (number <= 10) {
            synchronized (lock) {
                while (!isOddTurn) {
                    try {
                        lock.wait(); // 等待偶数线程打印完成后通知
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (number % 2 != 0) {
                    System.out.println("Thread-" + Thread.currentThread().getId() + "：" + number);
                    number++;
                    isOddTurn = false; // 切换到偶数回合
                    lock.notify(); // 通知偶数线程
                }
            }
        }
    }

    public void printEven() {
        while (number <= 10) {
            synchronized (lock) {
                while (isOddTurn) {
                    try {
                        lock.wait(); // 等待奇数线程打印完成后通知
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (number % 2 == 0) {
                    System.out.println("Thread-" + Thread.currentThread().getId() + "：" + number);
                    number++;
                    isOddTurn = true; // 切换到奇数回合
                    lock.notify(); // 通知奇数线程
                }
            }
        }
    }

    public static void main(String[] args) {
        OddEvenPrinter printer = new OddEvenPrinter();

        Thread oddThread = new Thread(printer::printOdd, "OddThread");
        Thread evenThread = new Thread(printer::printEven, "EvenThread");

        oddThread.start();
        evenThread.start();
    }
}